﻿using KamRemakeRemake.GameObjects.MapObjects;
using Microsoft.Xna.Framework;
using KamRemakeRemake.GameObjects.UnitObjects.Units;

namespace KamRemakeRemake.GameObjects.UnitObjects.ActionObjects.Actions {
    public class Walk : UnitAction {
       
        protected Tile TargetPosition { get; set; }
        protected Direction TargetDirection { get; set; }
        protected double Speed { get; set; }

        /// <summary>
        /// Let the unit walk to the targetdirection in the specified direction. When the direction is null North is used. 
        /// the unit value should be set by properties later on 
        /// </summary>
        /// <param name="targetX"></param>
        /// <param name="targetY"></param>
        /// <param name="targetDirection"></param>
        public Walk(Unit unit, Tile targetPosition, ActionType? actionType = ActionType.Walk)
            : base(unit, actionType.Value) {
            this.TargetDirection = Direction.East;
            this.TargetPosition = targetPosition;
            this.Speed = unit.UnitSpeed;
        }

        /// <summary>
        /// move the unit to the target location if that cant be done, hold the unit
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Execute() {
            if ((int)this.Unit.Position.X == (int)this.TargetPosition.MapRectangle.X && (int)this.Unit.Position.Y == (int)this.TargetPosition.MapRectangle.Y) {
                this.Unit.DoAction(new Idle(this.Unit));
            }
            else {
                double newXPosition = Unit.Position.X;
                if ((int)Unit.Position.X < TargetPosition.MapRectangle.X) {
                    newXPosition = Unit.Position.X + this.Speed;
                }
                else if ((int)Unit.Position.X > TargetPosition.MapRectangle.X) {
                    newXPosition = Unit.Position.X - this.Speed;
                }

                double newYPosition = Unit.Position.Y;
                if ((int)Unit.Position.Y < TargetPosition.MapRectangle.Y) {
                    newYPosition = Unit.Position.Y + this.Speed;
                }
                else if ((int)Unit.Position.Y > TargetPosition.MapRectangle.Y) {
                    newYPosition = Unit.Position.Y - this.Speed;
                }

                Unit.Direction = TargetDirection;
                Unit.Position = new Vector2((float)newXPosition, (float)newYPosition);
            }
        }
    }
}
